package com.cloud.lan.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.lan.api.dto.req.SubmailTaskReq;
import com.cloud.lan.dao.SubmailTaskMapper;
import com.cloud.lan.entity.base.BaseResponse;
import com.cloud.lan.entity.model.dataobject.SubmailTaskRecordDO;
import com.cloud.lan.service.ILanSmsSendService;
import com.cloud.lan.service.ISubmailTaskService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * Created by zhouquan on 2022/9/18.
 */
@Slf4j
@Service
public class SubmailTaskServiceImpl implements ISubmailTaskService{

    @Resource
    private SubmailTaskMapper submailTaskMapper;

    @Resource
    private ILanSmsSendService lanSmsSendService;


    @Override
    public BaseResponse<Object> manageList(SubmailTaskReq req) {
        log.info("subamil任务.查询列表:{}", req);
        LambdaQueryWrapper<SubmailTaskRecordDO> qw = new LambdaQueryWrapper<>();
        qw.orderByDesc(SubmailTaskRecordDO::getId);
        Page<SubmailTaskRecordDO> page = new Page<>(req.getPageIndex(), req.getPageSize());
        IPage<SubmailTaskRecordDO> pageList = submailTaskMapper.selectPage(page, qw);
        return BaseResponse.success(pageList);
    }

    @Override
    public boolean hasExistHandingTask(SubmailTaskReq req) {
        List<Integer> handingStatus = new ArrayList<>();
        handingStatus.add(1);
        handingStatus.add(2);
        handingStatus.add(3);
        handingStatus.add(4);
        LambdaQueryWrapper<SubmailTaskRecordDO> qw = new LambdaQueryWrapper<>();
        qw.eq(SubmailTaskRecordDO::getTemplateId, req.getTemplateId());
        qw.eq(SubmailTaskRecordDO::getTagCode, req.getTagCode());
        qw.in(SubmailTaskRecordDO::getTaskStatus,handingStatus);
        qw.orderByDesc(SubmailTaskRecordDO::getId);
        List<SubmailTaskRecordDO> recordDOList = submailTaskMapper.selectList(qw);
        return CollectionUtils.isNotEmpty(recordDOList);
    }

    @Override
    public boolean merge(SubmailTaskRecordDO recordDO) {
        Date nowD = new Date();
        recordDO.setUpdateTime(nowD);
        if(Objects.isNull(recordDO.getId()) || recordDO.getId() <0){
            recordDO.setCreateTime(nowD);
            submailTaskMapper.insert(recordDO);
        }else{
            submailTaskMapper.updateById(recordDO);
        }
        return true;
    }


    @Override
    public SubmailTaskRecordDO queryById(Long id) {
        return submailTaskMapper.selectById(id);
    }

    //任务状态1-新建;2-数据初始化中;3-数据初始化完成;4-submail发送中;5-submail发送结束
    @Override
    public BaseResponse<Object> operate(SubmailTaskReq req) {
        SubmailTaskRecordDO recordDO = queryById(req.getId());
        if(Objects.isNull(recordDO)){
            return BaseResponse.error("SUB003",req.getId()+"任务不存在");
        }
        //初始化数据
        if(req.getOperateType() == 1){
            if(recordDO.getTaskStatus() !=1){
                return BaseResponse.error("SUB003",req.getId()+"任务不是新建状态");
            }
            BaseResponse<Object> baseResponse = lanSmsSendService.initTaskRecordData(req.getId());
            log.info("任务.初始化数据.res:[{}]", JSON.toJSONString(baseResponse));
            return baseResponse;
        }

        //发送
        if(req.getOperateType() == 2){
            if(recordDO.getTaskStatus() !=3){
                return BaseResponse.error("SUB003",req.getId()+"任务数据没有初始化完毕");
            }
            BaseResponse baseResponse = lanSmsSendService.applySend(req.getId());
            log.info("任务.申请任务发送.res:[{}]", JSON.toJSONString(baseResponse));
            return baseResponse;
        }
        return BaseResponse.error("SUBT001",req.getId()+"未知类型");
    }

}
